/**
 Copyright will be fixed soon.
 Author: Hiroshi Ota.
 $Date: $
 $Revision: $
*/

#include "BFRunningStateApp.h"

//BFRunningStateApp::BFRunningStateApp(int noOfPathRecognizer, int noOfDriveControl, IBFLog* log)
//: strategyArgorithm(0), argorithmContainer(noOfPathRecognizer, noOfDriveControl), loopCounter(0), logModule(log)
//{
//}

BFRunningStateApp::BFRunningStateApp(BFArgorithmContainer* ac, IBFLog* log)
: argorithmContainer(ac), loopCounter(0), logModule(log)
#ifndef _BRICKOS_
, simCon(0)
#endif
{
}

BFRunningStateApp::~BFRunningStateApp()
{
//	if (strategyArgorithm!=0) {
//		delete strategyArgorithm;
//	}
}

int BFRunningStateApp::execEntry(BFState* state)
{
	strategyArgorithm = argorithmContainer->getStrategy();
	if (strategyArgorithm==0) {
		// judgement should be set up before execEntry calling!
		return -1;
	}
	int result = this->setup(state);
//	result = strategyArgorithm->initialize(state);
	return result;
}

int BFRunningStateApp::execDo(BFState *state)
{
	loopCounter = 0;
	while (true) {
#ifndef _BRICKOS_
		simCon->notifyLoopCount(loopCounter);
#endif
		int result = this->update(loopCounter, state);
		result = this->judge(true, loopCounter, state);
		if (result==1) {
			break;
		} else if (result!=0) {
			return result;
		}
		result = this->drive(loopCounter, state);
		result = this->judge(false,loopCounter, state);
		if (result==1) {
			break;
		} else if (result!=0) {
			return result;
		}

		loopCounter++;

#ifndef _NOT_USE_LOGGING_IN_RUNNING
		logModule->log(state);
#endif
	}
	return 0;
}

int BFRunningStateApp::execExit(BFState *state)
{
	if (strategyArgorithm==0) {
		return -1;
	}
	int result = this->cleanup(state);
//	result = strategyArgorithm->terminate(state);
	return result;
}
